package com.ctg.itrdc.imageconsole.control.user;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.imageconsole.common.bean.CommonResult;
import com.ctg.itrdc.imageconsole.common.bean.ReturnCodeDefine;
import com.ctg.itrdc.imageconsole.common.util.TimeUtils;
import com.ctg.itrdc.imageconsole.data.dto.DmsAuthorityDTO;
import com.ctg.itrdc.imageconsole.data.dto.user.PasswordDTO;
import com.ctg.itrdc.imageconsole.data.dto.user.UserDTO;
import com.ctg.itrdc.imageconsole.servcie.user.UserService;
import com.ctg.itrdc.imageconsole.servcie.user.UserServiceAux;
import com.ctg.itrdc.imageconsole.util.SysManagerUtil;

@Controller
@RequestMapping("/users")
public class UserController {

	private static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Resource
	private UserService userService;

	@Resource
	private UserServiceAux userServiceAux;

	@RequestMapping(value = "/{userId}/password", method = RequestMethod.PUT)
	@ResponseBody
	public CommonResult updatePassword(@PathVariable("userId") Long userId, @RequestBody PasswordDTO password) {
		try {
			if (null == password || null == password.getOldPassword() || null == password.getNewPassword())
				throw new RuntimeException("Error : some parameter is null");
			userService.updatePassword(userId, password, this.getAuthority());
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
	@ResponseBody
	public CommonResult deleteUser(@PathVariable("userId") Long userId) {
		try {
			userService.deleteUser(userId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public CommonResult addUser(@RequestBody UserDTO user) {
		try {
			if (null == user || null == user.getUserName() || null == user.getPassword())
				throw new RuntimeException("Error : some parameter is null");
			userService.addUser(user, this.getAuthority());
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, null);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getUsers(@RequestParam(value = "userCode", required = false) String userCode,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		if (null == pageNum)
			pageNum = new Integer(1);
		if (null == pageSize)
			pageSize = new Integer(10);
		try {
			Object users = userServiceAux.selectUsers(userCode, pageNum, pageSize);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, users);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}

	}

	@RequestMapping(value = "/userCodeExists", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult userCodeExists(@RequestParam(value = "userCode", required = false) String userCode) {
		try {
			Integer exists = userService.userCodeExists(userCode) ? 1 : 0;
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, exists);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "/isAdmin", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult isAdmin() {
		try {
			boolean isDockerManager = SysManagerUtil.isRoleOfDockerManager();
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, isDockerManager);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}
	}

	@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public CommonResult getUser(@PathVariable("userId") Long userId) {
		try {
			Object object = userServiceAux.selectUser(userId);
			return new CommonResult(ReturnCodeDefine.Code.C1000, ReturnCodeDefine.Msg.M1000, object);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new CommonResult(ReturnCodeDefine.Code.C1200, e.getMessage(), null);
		}

	}

	/* --------- private functions --------- */

	private DmsAuthorityDTO getAuthority() {
		DmsAuthorityDTO authority = new DmsAuthorityDTO();
		// authority.setCreateUserId(WebUtils.getCtgUser().getSysUserId());
		authority.setCreateUserId(195163L);
		authority.setCreateTime(TimeUtils.getNowDateStr());
		authority.setLastUpdateUserId(authority.getCreateUserId());
		authority.setLastUpdateTime(authority.getCreateTime());
		return authority;
	}
}
